package com.yxq.mdt.app.modules.login.presenter;

import com.yxq.mdt.app.base.BasePresenter;
import com.yxq.mdt.app.modules.login.bean.UserBean;
import com.yxq.mdt.app.modules.login.model.LoginModel;
import com.yxq.mdt.app.modules.personal.bean.MainPersonTokenBean;
import com.yxq.mdt.app.modules.personal.bean.MainPersonalImNumber;
import com.yxq.mdt.app.modules.personal.bean.MainSaveAddImNumber;
import com.yxq.mdt.app.modules.personal.model.MainPersonalApi;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * ================================================
 *
 * @function 登录
 * Created by yd on 2019/03/26.
 * ================================================
 */
public class LoginPresenter extends BasePresenter<ILoginContract.View, LoginModel> implements ILoginContract.Presenter {

    public LoginPresenter(ILoginContract.View view, LoginModel model) {
        super(view, model);
    }


    @Override
    public void login(String account, String password) {

        mBaseModel.login(account, password, new ApiModel.CommonCallback<UserBean>() {
            @Override
            public void onSuccess(UserBean bean) {
                mRootView.onLoginSuccess(bean);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onLoginFailed(errMsg);
            }
        });
    }

    public void loginCode(String account, String code) {

        mBaseModel.loginCode(account, code, new ApiModel.CommonCallback<UserBean>() {
            @Override
            public void onSuccess(UserBean bean) {
                mRootView.onLoginSuccess(bean);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onLoginFailed(errMsg);
            }
        });
    }

    public void verifyCode(String account, int sms_sce) {
        mBaseModel.verifyCode(account, sms_sce, new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String s) {
                mRootView.onVerifyCodeSuccess(s);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onVerifyCodeFailed(errMsg);
            }
        });
    }

    public void register(String account, String password, String password2, int platform, String sms_code,String invite) {
        mBaseModel.register(account, password, password2, platform, sms_code,invite, new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String userBean) {
                mRootView.onRegisterSuccess(userBean);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onRegisterFailed(errMsg);
            }
        });
    }

    public void forget(String account, String password, String password2, int platform, String sms_code) {
        mBaseModel.forget(account, password, password2, platform, sms_code, new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String userBean) {
                mRootView.onRegisterSuccess(userBean);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onRegisterFailed(errMsg);
            }
        });
    }

    public void loginApp3(String openid, String nickname, String avatar_url, String platform) {
        mBaseModel.loginAPP3(openid, nickname, avatar_url, platform, new ApiModel.CommonCallback<UserBean>() {
            @Override
            public void onSuccess(UserBean userBean) {
                mRootView.onLoginSuccess(userBean);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onLoginFailed(errMsg);
            }
        });
    }

    public void bindPhone(String account, String password, String token, String sms_code) {
        mBaseModel.bindPhone(account, password, token, sms_code, new ApiModel.CommonCallback<String>() {
            @Override
            public void onSuccess(String userBean) {
                mRootView.onBindPhoneSuccess(userBean);
            }

            @Override
            public void onError(String errMsg) {
                mRootView.onBindPhoneFailed(errMsg);
            }
        });
    }

    /**
     * 创建Java Token
     *
     * @param token
     * @param callback
     */
    public void getTokenData(String token, ApiModel.CommonCallback<MainPersonTokenBean> callback) {
        RetrofitFactory.getJavaRetrofit().create(MainPersonalApi.class)
                .getToken(token)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MainPersonTokenBean>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(MainPersonTokenBean respBase) {
                        callback.onSuccess(respBase);
                    }
                });
    }

    /**
     * IM用户操作-新增、修改IM账号
     *
     * @param nikeName
     * @param callback
     */
    public void getSaveImUser(String nikeName, ApiModel.CommonCallback<MainPersonalImNumber> callback) {
        RetrofitFactory.getJavaRetrofit().create(MainPersonalApi.class)
                .getSaveImUser(nikeName)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MainPersonalImNumber>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(MainPersonalImNumber respBase) {
                        callback.onSuccess(respBase);
                    }
                });
    }

    /**
     * 获取im账号
     *
     * @param callback
     */
    public void getImNumberData(ApiModel.CommonCallback<MainPersonalImNumber> callback) {
        RetrofitFactory.getJavaRetrofit().create(MainPersonalApi.class)
                .getImData()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<MainPersonalImNumber>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    protected void onSuccess(MainPersonalImNumber baseEntity) throws Exception {
                        callback.onSuccess(baseEntity);
                    }

                });
    }
}
